﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;

namespace Cad2018Tool
{
    public class CadTool
    {
        public CadTool()
        {
            var _cad = new ConnectCad().CadApplication();
            ThisDrawing = _cad.ActiveDocument;
        }

        // cad当前打开的图纸
        private AcadDocument ThisDrawing = null;
      
        /// <summary>
        /// 创建直线
        /// </summary>
        /// <returns></returns>
        public bool CreateLine()
        {
            try
            {
                double[] startPoint = new double[] { 0, 0, 0 };
                double[] endPoint = new double[] { 1000, 0, 0 };
                // 添加一根直线
                ThisDrawing.ModelSpace.AddLine(startPoint, endPoint);
                double[] polyline_2dPoints = new double[]
                {
                    0,0,
                    0,1000,
                    1000,0
                };
                ThisDrawing.ModelSpace.AddLightWeightPolyline(polyline_2dPoints);
                double[] polyline_3dPoint = new double[]
                {
                    0,0,0,
                    1000,1000,0
                };
                ThisDrawing.ModelSpace.AddPolyline(polyline_3dPoint);

                // ThisDrawing.ModelSpace.AddText();

                // 创建圆
                double[] centerPoint = new double[] { 0, 0, 0 };
                double radius = 100;
                ThisDrawing.ModelSpace.AddCircle(centerPoint, radius);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 选择对象
        /// 图层，颜色，线型
        /// </summary>
        public void SelectObj(out double radius, out double[] point)
        {
            radius = 0;
            point = new double[] { };
            try
            {
                List<string> allSetNames = new List<string>();
                var setEnum = ThisDrawing.SelectionSets.GetEnumerator();
                setEnum.Reset();
                while (setEnum.MoveNext())
                {
                    allSetNames.Add(((AcadSelectionSet)setEnum.Current).Name);
                }

                string setName = "set";
                while (allSetNames.Contains(setName))
                {
                    setName += "1";
                }

                AcadSelectionSet selectionSet = ThisDrawing.SelectionSets.Add(setName);
                Int16[] filterType = new short[] { 8 };
                object[] filterData = new object[] { "0" };
                selectionSet.SelectOnScreen(filterType, filterData);

                foreach (var item in selectionSet)
                {
                    if (item is AcadCircle)
                    {
                        radius = ((AcadCircle)item).Radius;
                        point = ((AcadCircle)item).Center;
                    }
                }
            }
            catch (Exception)
            {
            }
        }

    }
}
